package transport

import (
	"context"
	"encoding/json"
	"errors"
	"go-micro-kit/EndPoints"
	"net/http"
)

// 当请求过来，对请求进行decode
func DecodeUserRequest(c context.Context, r *http.Request) (interface{},error){
	username:= r.URL.Query().Get("username")
	password:= r.URL.Query().Get("password")
	if username!=""{
		return  EndPoints.UserRequest{
			// 这里可以再传一个Method，判断是get还是post
			Username:username,
			Password:password,
		},nil
	}
	return nil,errors.New("参数错误")
}
// 对返回值进行编码，编码成json或者protobuf等
func EncodeUserResponse(c context.Context, w http.ResponseWriter, response interface{}) error  {
	// 设定json返回格式
	w.Header().Set("Content-type","application/json")
	return json.NewEncoder(w).Encode(response)
}